package com.noble.glamour.module.logic.baseui.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.noble.meether.R;

/**
 * 设置文字时，文字先变大后缩小为起始大小
 * Created by Su on 2017/7/22.
 */
public class ScaleTextView extends AppCompatTextView {
    private static float DEFAULT_SIZE = 13;         // 默认文字大小
    private static final float DEFAULT_LARGE_SIZE = 20;   // 最大字体大小

    private Handler mHandler;
    private final int SHOW_NEXT = 1;
    private long mDuration = 120;   // 重置时间间隔

    private String text;
    private float textSize = DEFAULT_SIZE; // 文字大小
    private int textColor;               // 文字颜色
    private float mInterval = 0.8f;      // 文字size自增
    private boolean isStarted = false;   // 是否已开始动画
    private boolean isShrink = false;    // 是否开始收缩


    public ScaleTextView(Context context) {
        super(context);
        init();
    }

    public ScaleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ScaleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ty = context.obtainStyledAttributes(attrs, R.styleable.ScaleTextView, defStyleAttr, 0);
        textSize = ty.getDimensionPixelSize(R.styleable.ScaleTextView_textSize, (int) DEFAULT_SIZE);
        ty.recycle();
        init();
    }

    private void init() {
        mHandler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                if (msg.what == SHOW_NEXT) {
                    setTextSize(textSize);
                    setText(text);
                    if (isStarted) {
                        sendEmptyMessageDelayed(SHOW_NEXT, mDuration);
                    }

                    // 放大文字
                    if (!isShrink) {
                        textSize += mInterval;

                        isShrink = textSize >= DEFAULT_LARGE_SIZE;
                        return;
                    }

                    // 缩小文字
                    textSize -= mInterval;
                    if (textSize <= DEFAULT_SIZE)
                        stop();
                }
            }
        };
    }

    public void setScaleTextSize(int textSize) {
        this.textSize = DEFAULT_SIZE = textSize;
    }

    public void setScaleTextColor(int textColor) {
        this.textColor = textColor;
    }

    public void setScaleText(String text) {
        isStarted = true;
        this.text = text;
        setText(text);
        if (mHandler != null)
            mHandler.sendEmptyMessage(SHOW_NEXT);
    }

    private void stop() {
        isShrink = false;
        isStarted = false;
        if (mHandler != null)
            mHandler.removeMessages(SHOW_NEXT);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stop();
        mHandler = null;
    }
}
